#include <dbg/ptest.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <metalc/base/mtls.h>
#include <metalc/mlos/mmapheap.h>
/* */
// #include <metalc/metalc.h>

/* 128KB, 可以容纳多少个线程? */
DEF_POBJ_PHEAP(mlcc, 128 * 1024)
SAI void *mlcc_palloc(size_t size){ return pheap_palloc_mlcc(size); }

// DEF_POBJ_PHEAP(mlcc, 128 * 1024)
// SAI void *mlcc_palloc(size_t size){ return pheap_palloc_mlcc(size); }


DEF_MMAPHEAP_MLAYER(mmapheap_normal, 2 * 1024 * 1024, SIZE_1G, SIZE_64G, 1, 0)

COMPOSE_MLAYER(test_mmapheap, mmapheap_normal)


DEF_MTLS_PTHREAD(GET_MLAYER_OBJNR(test_mmapheap), 4096, mlcc_palloc, mp_coreid)


#include <pthreadalt/pthreadalt_init.h>
DEF_PTHREADALT_INIT
DEF_SYSINIT(test_mmapheap, mtls)
DEF_CFLOWFIN(mtls)
DEF_CFLOWINIT(test_mmapheap, mtls)
DEF_SYSFIN(__omega)

void test_mmapheap(){
	mreq_t req;
	void *mem;
	mreq_setalloc(&req, 4096, 4096);
	mem = test_mmapheap_alloc(&req);
	assert(mem == req.ptr);
	assert(mem);
	assert(req.size == SIZE_2M); /* 等于对齐大小 */
	memset(mem, 0xff, req.size);
	word_t *parr = (word_t *)mem;
	assert(atm_ld(&(parr[256])) == (word_t)(-1));
	bool_t s = test_mmapheap_free(&req);
	assert(s == 0);
}

int main(){
	test_mmapheap();
	return 0;
}